<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Controller
 *
 * @author Zelic
 */
class Lib_Controller {

    var $model;
    var $view;
    var $className;
    var $layoutName = "default";
    var $moduleName;
    // Data for rendering action view
    var $data;
    // View components
    var $component;
    var $renderedComponent;
    // Actions need user to auth
    var $restrictedAction;
    // Controller which has access to user roles
    var $authController;
    // Check users whether they is auth or not
    var $authEngine;

    function __construct() {
        $part = explode('_', get_class($this));
        $this->moduleName = $part[1];
        $part[2] = 'Model';
        $this->className = $part[3];
        $modelName = join('_', $part);
        $this->model = new $modelName;
        $this->view = new Lib_View();
        $this->component = array();
        $this->restrictedAction = array();
    }

    function setAuthEngine($authEngine) {
        $this->authEngine = $authEngine;
    }

    function setAuthController($authController) {
        $this->authController = $authController;
    }

    function setComponent($component) {
        $this->component[$component] = new $component;
    }

    function renderComponent() {
        foreach ($this->component as $component) {
            $componentName = 'App_' . $this->moduleName . '_Component_' . $component;
            $componentObject = new $componentName($this->moduleName);
            $this->renderedComponent[$component] = $componentObject->render();
        }
    }

    function beforeAction() {
        
    }

    function afterAction() {
        
    }

    function execute($action, $data = null) {
        $isOk = false;
        
        $module = strtolower($this->moduleName);
        
        if (empty($action)) {
            echo "No action specified";
            return;
        }
        $listActions = get_class_methods(get_class($this));

        if (in_array($action, $listActions)) {
            if (isset($this->restrictedAction[$action])) {
                if ($this->authEngine->checkAuth()==true) {
                    $isOk = true;
                } else {
                    $this->redirect(Lib_Controller::getRedirectUrl($this->authController, 'login'));
                }
            } else {
                $isOk = true;
            }
            if ($isOk) {
                $this->beforeAction();
                if (is_array($data)) {
                    $this->$action($data);
                } else {
                    $this->$action();
                }
                $this->afterAction();
                if (is_null($this->view)) {
                    echo "Here";
                    $this->renderedComponent['mainContent'] = "No view for this action";
                } else {
                    $this->renderedComponent['mainContent'] = $this->view->renderAction($this->className, $module, $action, $this->data);
                }
                $this->renderComponent();
                return $this->view->renderPage($this->renderedComponent, $module, $this->layoutName);
            }
        } else {
            return "Page not found!";
        }
    }

    function redirect($url) {
        header("Location: http://" . Lib_Registry::$data['host'] . Lib_Registry::$data['root'] . "/$url");
    }
    
    static function getRedirectUrl($controllerObject, $action){
        $controllerName = get_class($controllerObject);
        $controllerName = strtolower($controllerName);
        $part = explode('_', $controllerName);
        $module = $part[1];
        $controller = $part[3];
        return $module.'/'.$controller.'/'.$action;
    }

}

?>
